package kz.edu.sdu.itech.pcs.server;

import java.io.IOException;
import java.net.Socket;

import org.apache.log4j.Logger;

public class WorkerListener extends Thread implements Runnable {

   public static final int SLEEP = 1000;

   static Logger log = Logger.getLogger(WorkerListener.class);

   Server server;

   public WorkerListener() {

   }

   public WorkerListener(Server server) {
      this.server = server;
   }

   @Override
   public void run() {
      log.info("Listen for worker connections");
      while (!AppProps.DONE) {
         if (AppProps.ACCEPT_CLIENTS) {
            try {
               Socket clientSocket = server.workingSocket.accept();
               Worker worker = new Worker(clientSocket, server);
               log.debug("New worker came in");
               server.workerSet.add(worker);
               worker.getThread().start();
            } catch (IOException e) {
//               e.printStackTrace();
            }
         } else {
            sleep(1000);
         }
      }
   }

   public void sleep(int x) {
      try {
         Thread.sleep(x);
      } catch (Exception e) {

      }
   }
}
